package com.axino.accessPlatform.web;

import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.util.Map;
import java.util.Timer;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * 定时刷新token
 * @author fzc
 * @package com.axino.accessPlatform.web
 * @create 2018-04-23 下午1:38
 **/
public class RefreshTokenTimer implements Runnable{

    private static final int threadPoolSize = 1;

    ScheduledExecutorService executor = Executors.newScheduledThreadPool(threadPoolSize);

    private String _url; //获取token地址
    private String _header;
    private String client_secret; //创建应用后，分配给应用的appSecret

    private String refresh_token; //刷新令牌
    private String client_id; //获取access_token时授权商户的userId
    private String grant_type = "refresh_token";   //授权类型，此值固定为“refresh_token”
    private long delay;
    private long preiod;

    private Timer timer = new Timer();

    public RefreshTokenTimer(long delay, long preiod, String url, String header, String refresh_token,
                             String client_secret, String client_id) {
        this._url = url;
        this._header = header;
        this.refresh_token = refresh_token;
        this.client_secret = client_secret;
        this.client_id = client_id;
        this.delay = delay;
        this.preiod = preiod;
    }

    void executeTimer() {
        executor.scheduleAtFixedRate(new RefreshTokenTimer(delay, preiod, _url, _header,
                refresh_token, client_secret, client_id), delay, preiod, TimeUnit.SECONDS);
    }

    @Override
    public void run() {
        try {
            CloseableHttpClient client = HttpClients.createDefault();
            HttpUriRequest build = RequestBuilder
                    .post(_url)
                    .addHeader("Content-Type", _header)
                    .addParameter("refresh_token", refresh_token)
                    .addParameter("client_id", client_id)
                    .addParameter("client_secret", client_secret)
                    .addParameter("grant_type", grant_type)
                    .build();

            //请求
            CloseableHttpResponse response = client.execute(build);
            HttpEntity entity = response.getEntity();

            String resValue = "";
            if (entity != null) {
                resValue = EntityUtils.toString(entity);
                System.out.println("refresh_toke_response: " + resValue);
            }

            if (!StringUtils.isEmpty(resValue)) {
                Map<String, Object> map = Oauth2AccessToken.parseJSON2Map(resValue);
                refresh_token = String.valueOf(map.get("refresh_token"));
            }

            //释放资源
            EntityUtils.consume(entity);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
//        new RefreshTokenTimer(5000L, 5000L, "https://opentest.jss.com.cn/accessToken",
//                "application/x-www-form-urlencoded", "",
//                String.valueOf(map.get("refresh_token")), String.valueOf(map.get("userId"))).executeTimer();
    }
}

